package bangry.chat.server.control;

import java.util.Enumeration;
import java.util.Hashtable;

public class ChatRoom {
	
	private String roomName; // 방이름(유일값)
	private int roomSize;    // 방사이즈
	private String captin;   // 방장이름
	private boolean open;    // 공개여부
	
	
	// 입장한 클라이언트 관리
	private Hashtable<String, ConnectionChatClient> userList;
	
	// 방장관리
	private Hashtable<String, ConnectionChatClient> captins;
		
	
	public ChatRoom(String roomName, String captin) {
		this(roomName, 10, captin, true);
	}
	
	public ChatRoom(String roomName, int roomSize, String captin, boolean open) {
		this.roomName = roomName;
		this.roomSize = roomSize;
		this.captin = captin;
		this.open = open;
		userList = new Hashtable<String, ConnectionChatClient>(roomSize);
	}
	
	
	
	// 방입장
	public void enterChatRoom(String nickName,  ConnectionChatClient client){
		userList.put(nickName, client);
		System.out.println("입장 후 방의 유저수:"+userList.size());
		
	}
	
	// 방퇴장
	public void leavingChatRoom(String nickName){
		userList.remove(nickName);
		System.out.println("퇴장 후 방의 유저수:"+userList.size());
	}
		
	// 현재방에 입장해 있는 모든 클라이언트들에게 메시지 전송
	public void sendAllMessage(String message){
		Enumeration<ConnectionChatClient> e = userList.elements();
		while(e.hasMoreElements()){
			ConnectionChatClient client  = e.nextElement();
			client.sendMessage(message);
		}
	}	
	
	
	// 현재방에 입장해 클라이언트 목록 전송
	public String getChatClientList(){
		StringBuffer sb = new StringBuffer();
		for(ConnectionChatClient client : userList.values()){
			sb.append(client.getNickName() + "/");
		}		
		return sb.toString();
	}
	
	// 방장 등록
	public void registCaptin(String nickName, ConnectionChatClient client){
		captins.put(nickName, client);
	}
	
	// 방장 삭제
	public void removeCaptin(String nickName){
		captins.remove(nickName);
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public int getRoomSize() {
		return roomSize;
	}

	public void setRoomSize(int roomSize) {
		this.roomSize = roomSize;
	}

	public String getCaptin() {
		return captin;
	}

	public void setCaptin(String captin) {
		this.captin = captin;
	}

	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}
	
	
}
